package com.cc.user.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cc.common.utils.ResponseData;
import com.cc.common.utils.ResultMsg;
import com.cc.user.entity.UserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.cc.user.entity.ScheduleEntity;
import com.cc.user.service.ScheduleService;
import com.cc.common.utils.R;

import javax.servlet.http.HttpSession;


/**
 * 
 *
 * @author leifengyang
 * @email leifengyang@gmail.com
 * @date 2020-12-11 11:30:58
 */
@RestController
@RequestMapping("user/schedule")
@Api(tags="待办")
public class ScheduleController {
    @Autowired
    private ScheduleService scheduleService;


    @GetMapping("/updateNotreadinfoState")
    @ApiOperation("根据待办id更改未读为已读")
    @ApiImplicitParam(name = "sid",value = "待办id",paramType = "query",dataType = "Long",required = true)
    public ResponseData updateNotreadinfoState(@RequestParam("sid") Long sid) {
        ScheduleEntity scheduleEntity = new ScheduleEntity();
        scheduleEntity.setScheduleStatus(1);
        //根据待办id更改未读为已读
        boolean flag = scheduleService.update(scheduleEntity, new QueryWrapper<ScheduleEntity>().eq("sid",sid));
        if (!flag){
            return ResponseData.success("更新失败",false);
        }
        return ResponseData.success("更新成功",true);
    }


    @GetMapping("/deleteinfo")
    @ApiOperation("删除待办信息")
    public ResponseData deleteInfo(@RequestParam("sid")Long sid) {
        //根据待办信息sid删除信息
        int flag = scheduleService.deleteInfo(sid);

        if (flag <= 0) {
            return ResponseData.success("删除失败",false);
        }
        return ResponseData.success("删除成功",true);
    }


    @GetMapping("/getAllInfo")
    @ApiOperation("获取所有待办消息")
    public ResponseData<List<ScheduleEntity>> getAllInfo(HttpSession session) {
        UserEntity user = (UserEntity) session.getAttribute("loginuser");
        Long uid = user.getUid();

        //根据uid、状态查询未读所有消息
        List<ScheduleEntity> list= scheduleService.list(new QueryWrapper<ScheduleEntity>().eq("uid",uid).orderByDesc("createtime"));

        return ResponseData.success(list);
    }



    @GetMapping("/getnotreadinfo")
    @ApiOperation("获取未读所有消息")
    public ResponseData<List<ScheduleEntity>> getNotreadInfo(HttpSession session) {
        UserEntity user = (UserEntity) session.getAttribute("loginuser");
        Long uid = user.getUid();
        //根据uid、状态查询未读所有消息
        List<ScheduleEntity> list= scheduleService.getAllNotreadInfo(uid, 0);

        return ResponseData.success(list);
    }

    @GetMapping("/getreadinfo")
    @ApiOperation("获取已读所有消息")
    public ResponseData<List<ScheduleEntity>>  getReadInfo(HttpSession session) {
        UserEntity user = (UserEntity) session.getAttribute("loginuser");
        Long uid = user.getUid();

        //根据uid、状态查询已读所有消息
        List<ScheduleEntity> list= scheduleService.getAllNotreadInfo(uid, 1);

        return ResponseData.success(list);
    }


    /**
     * 列表
     */


//    /**
//     * 信息
//     */
//    @RequestMapping("/info/{sid}")
//    //@RequiresPermissions("user:schedule:info")
//    public R info(@PathVariable("sid") Long sid){
//		ScheduleEntity schedule = scheduleService.getById(sid);
//
//        return R.ok().put("schedule", schedule);
//    }
//
    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody ScheduleEntity schedule){
		scheduleService.save(schedule);
        return R.ok();
    }
//
//    /**
//     * 修改
//     */
//    @RequestMapping("/update")
//    //@RequiresPermissions("user:schedule:update")
//    public R update(@RequestBody ScheduleEntity schedule){
//		scheduleService.updateById(schedule);
//
//        return R.ok();
//    }
//
//    /**
//     * 删除
//     */
//    @RequestMapping("/delete")
//    //@RequiresPermissions("user:schedule:delete")
//    public R delete(@RequestBody Long[] sids){
//		scheduleService.removeByIds(Arrays.asList(sids));
//
//        return R.ok();
//    }

}
